package com.meituan.sqt.mock.infrastructure.persistence.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.mapper.TypeBOrderInfoMapper;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.mapper.TypeBPayInfoMapper;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.mapper.TypeBRefundInfoMapper;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeBOrderInfoPo;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeBPayInfoPo;
import com.meituan.sqt.mock.infrastructure.persistence.mysql.po.TypeBRefundInfoPo;
import com.meituan.sqt.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: chenzhenfeng@meituan.com
 * @date: 2023/1/30 16:50
 * @description:
 */
@Service
@Slf4j
public class TypeBRepository {
    @Resource
    private TypeBOrderInfoMapper orderInfoMapper;
    @Resource
    private TypeBPayInfoMapper payInfoMapper;
    @Resource
    private TypeBRefundInfoMapper refundInfoMapper;


    // 订单相关
    public TypeBOrderInfoPo queryBySqtBizOrderId(Long sqtBizOrderId) {
        Preconditions.checkNotNull(sqtBizOrderId);
        QueryWrapper<TypeBOrderInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sqt_biz_order_id", sqtBizOrderId);
        return orderInfoMapper.selectOne(queryWrapper);
    }

    public TypeBOrderInfoPo queryByOrderId(Long orderId) {
        Preconditions.checkNotNull(orderId);
        return orderInfoMapper.selectById(orderId);
    }

    public void saveOrderInfo(TypeBOrderInfoPo po) {
        orderInfoMapper.insert(po);
        log.info("【B类模型】保存订单记录, orderInfo:{}", JsonUtil.object2Json(po));
    }

    // 支付相关
    public TypeBPayInfoPo queryByTradeNo(Long tradeNo) {
        Preconditions.checkNotNull(tradeNo);
        QueryWrapper<TypeBPayInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trade_no", tradeNo);
        return payInfoMapper.selectOne(queryWrapper);
    }

    public void savePayInfo(TypeBPayInfoPo payInfoPo) {
        payInfoMapper.insert(payInfoPo);
        log.info("【B类模型】保存支付记录, payInfoPo:{}", JsonUtil.object2Json(payInfoPo));
    }

    // 退款相关
    public TypeBRefundInfoPo queryByRefundTradeNo(Long refundTradeNo) {
        Preconditions.checkNotNull(refundTradeNo);
        QueryWrapper<TypeBRefundInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("refund_trade_no", refundTradeNo);
        return refundInfoMapper.selectOne(queryWrapper);
    }

    public List<TypeBRefundInfoPo> listRefundInfosByPayId(Long payId) {
        QueryWrapper<TypeBRefundInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_id", payId);
        return refundInfoMapper.selectList(queryWrapper);
    }

    public List<TypeBRefundInfoPo> listRefundInfosByOrderId(Long orderId) {
        QueryWrapper<TypeBRefundInfoPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return refundInfoMapper.selectList(queryWrapper);
    }

    public void saveRefundInfo(TypeBRefundInfoPo refundInfoPo) {
        refundInfoMapper.insert(refundInfoPo);
        log.info("【B类模型】保存退款记录, refundInfo:{}", JsonUtil.object2Json(refundInfoPo));
    }
}
